﻿using PhotoGallery.Data;
using PhotoGallery.Data.Entity;
using PhotoGallery.Data.Repository;
using PhotoGallery.Infrastructure;
using PhotoGallery.Models.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;

namespace PhotoGallery.Models
{
    public class VideoAttacumentsInfo
    {
        public int ID { get; set; }
        public string FileName { get; set; }
        public int VideoID { get; set; }
        [LocalizedDisplayName("date", NameResourceType = typeof(Names))]
        public DateTime? UploadDate { get; set; }

        public string Url { get; set; }        
        public string FullFileName { get; set; }
        public string MimeType 
        {
            get
            {
                string ext = Path.GetExtension(FileName).ToLower();
                switch (ext)
                {
                    case ".webm":
                        return "video/webm";
                    case ".ogv":
                        return "video/ogg";
                    default:
                        return "video/mp4";
                }
            }
        }
    }

    public class VideoAttacmentModel : BaseModel
    {
        string path, url;
        Album album;
        private VideoAttachmentsRepository repository;

        public VideoAttacmentModel(int albumID, int videoID)
        {
            album = new AlbumRepository(Owner).Get(albumID);
            path = BasePath + Settings.UploadsFolder + Owner + "\\Videos\\" + getAlbumFolder(album);
            url = Settings.UploadsUrl + Owner + "/Videos/" + getAlbumUrl(album);

            repository = new VideoAttachmentsRepository(albumID, videoID, Owner);
        }

        public VideoAttacmentModel(int albumID, int videoID, string owner, string albumFolder)
        {
            album = new AlbumRepository(owner).Get(albumID);
            path = BasePath + Settings.UploadsFolder + owner + "\\Videos\\" + albumFolder + "\\";
            url = Settings.UploadsUrl + owner + "/Videos/" + albumFolder + "/";

            repository = new VideoAttachmentsRepository(albumID, videoID, owner, albumFolder);
        }

        public IEnumerable<VideoAttacumentsInfo> Get()
        {
            List<VideoAttacumentsInfo> videos = new List<VideoAttacumentsInfo>();
            var data = repository.Get();
            foreach (var entity in data)
            {
                var item = new VideoAttacumentsInfo()
                {
                    ID = entity.ID,
                    FileName = entity.FileName,
                    VideoID = entity.VideoID,
                    UploadDate = entity.UploadDate,

                    Url = url + entity.FileName,
                    FullFileName = path + entity.FileName
                };
                videos.Add(item);
            }
            return videos;
        }

        public VideoAttacumentsInfo Get(int id)
        {
            var entity = repository.Get(id);
            if (entity != null)
            {
                var item = new VideoAttacumentsInfo()
                {
                    ID = entity.ID,
                    FileName = entity.FileName,
                    VideoID = entity.VideoID,
                    UploadDate = entity.UploadDate,

                    Url = url + entity.FileName,
                    FullFileName = path + entity.FileName
                };
                return item;
            }
            return null;
        }

        public VideoAttacumentsInfo GetFirst()
        {
            var entity = repository.GetFirst();
            if (entity != null)
            {
                var item = new VideoAttacumentsInfo()
                {
                    ID = entity.ID,
                    FileName = entity.FileName,
                    VideoID = entity.VideoID,
                    UploadDate = entity.UploadDate
                };
                return item;
            }
            return null;
        }

        public void Create(VideoAttacumentsInfo video, HttpPostedFileBase file)
        {
            if (file == null || file.FileName == string.Empty)
            {
                return;
            }
            string fileName = Path.GetFileName(file.FileName);
            video.FileName = FileUtils.CheckFileExists(file, path, fileName);
            file.SaveAs(path + video.FileName);

            var entity = new VideoAttachments()
            {
                //ID = video.ID,
                FileName = video.FileName,
                VideoID = video.VideoID,
                UploadDate = video.UploadDate
            };
            repository.Create(entity);

        }

        public void Update(VideoAttacumentsInfo video, HttpPostedFileBase file, HttpPostedFileBase file1)
        {
            if (file != null)
            {
                video.FileName = FileUtils.CheckFileExists(file, path);
                file.SaveAs(path + video.FileName);
            }

            var entity = new VideoAttachments()
            {
                ID = video.ID,
                FileName = video.FileName,
                VideoID = video.VideoID,
                UploadDate = video.UploadDate
            };
            repository.Update(entity);
        }

        public void Delete(int id)
        {
            repository.Delete(id);
        }

    }
}